package controller

import (
	"YingShop/entity"
	querystruct "YingShop/queryStruct"
	"strconv"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

type ShopOrderController struct {
	Result
}

func (orderController ShopOrderController) Get(ctx *gin.Context) {
	orderId, _ := strconv.Atoi(ctx.Param("orderId"))
	order := entity.ShopOrder{OrderId: orderId}
	err := order.Load()
	if err != nil {
		orderController.RetCode = -1
		orderController.Msg = err.Error()
	} else {
		orderProductList, err := order.GetOrderProductList()
		if err != nil {
			orderController.RetCode = -1
			orderController.Msg = err.Error()
		} else {
			orderController.RetCode = 0
			orderController.Msg = "success"
			orderController.Data = gin.H{
				"orderInfo":   order,
				"productList": orderProductList,
			}
		}
	}
	ctx.JSON(200, orderController.Result)
}

func (orderController ShopOrderController) Post(ctx *gin.Context) {
	orderCreateParam := querystruct.OrderCreateParam{}
	if err := ctx.ShouldBindJSON(&orderCreateParam); err != nil {
		orderController.RetCode = -1
		orderController.Msg = err.Error()
		ctx.JSON(200, orderController.Result)
		return
	}
	session := sessions.Default(ctx)
	userName, _ := session.Get("userName").(string)
	userId, _ := session.Get("userId").(int)
	userOrder := entity.ShopOrder{OrderCreator: userName, OrderCreatorId: userId}
	err := userOrder.Create(orderCreateParam)
	if err != nil {
		orderController.RetCode = -1
		orderController.Msg = err.Error()
		ctx.JSON(200, orderController.Result)
		return
	}
	orderController.Data = gin.H{
		"orderId": userOrder.OrderId,
	}
	ctx.JSON(200, orderController.Result)
}

func (orderController ShopOrderController) Pay(ctx *gin.Context) {
	orderId, _ := strconv.Atoi(ctx.Param("orderId"))
	userOrder := entity.ShopOrder{OrderId: orderId}
	err := userOrder.Load()
	if err != nil {
		orderController.RetCode = -1
		orderController.Msg = err.Error()
		ctx.JSON(200, orderController.Result)
		return
	}
	err = userOrder.Pay()
	if err != nil {
		orderController.RetCode = -1
		orderController.Msg = err.Error()
		ctx.JSON(200, orderController.Result)
		return
	}
	orderController.Data = gin.H{
		"orderId": userOrder.OrderId,
	}
	ctx.JSON(200, orderController.Result)

}

func (orderController ShopOrderController) Accept(ctx *gin.Context) {
	orderId, _ := strconv.Atoi(ctx.Param("orderId"))
	userOrder := entity.ShopOrder{OrderId: orderId}
	err := userOrder.Load()
	if err != nil {
		orderController.RetCode = -1
		orderController.Msg = err.Error()
		ctx.JSON(200, orderController.Result)
		return
	}
	err = userOrder.Accept()
	if err != nil {
		orderController.RetCode = -1
		orderController.Msg = err.Error()
		ctx.JSON(200, orderController.Result)
		return
	}
	orderController.Data = gin.H{
		"orderId": userOrder.OrderId,
	}
	ctx.JSON(200, orderController.Result)

}

func (orderController ShopOrderController) Making(ctx *gin.Context) {
	orderId, _ := strconv.Atoi(ctx.Param("orderId"))
	userOrder := entity.ShopOrder{OrderId: orderId}
	err := userOrder.Load()
	if err != nil {
		orderController.RetCode = -1
		orderController.Msg = err.Error()
		ctx.JSON(200, orderController.Result)
		return
	}
	err = userOrder.Making()
	if err != nil {
		orderController.RetCode = -1
		orderController.Msg = err.Error()
		ctx.JSON(200, orderController.Result)
		return
	}
	orderController.Data = gin.H{
		"orderId": userOrder.OrderId,
	}
	ctx.JSON(200, orderController.Result)

}

func (orderController ShopOrderController) Ready(ctx *gin.Context) {
	orderId, _ := strconv.Atoi(ctx.Param("orderId"))
	userOrder := entity.ShopOrder{OrderId: orderId}
	err := userOrder.Load()
	if err != nil {
		orderController.RetCode = -1
		orderController.Msg = err.Error()
		ctx.JSON(200, orderController.Result)
		return
	}
	err = userOrder.Ready()
	if err != nil {
		orderController.RetCode = -1
		orderController.Msg = err.Error()
		ctx.JSON(200, orderController.Result)
		return
	}
	orderController.Data = gin.H{
		"orderId": userOrder.OrderId,
	}
	ctx.JSON(200, orderController.Result)

}

func (orderController ShopOrderController) Finish(ctx *gin.Context) {
	orderId, _ := strconv.Atoi(ctx.Param("orderId"))
	userOrder := entity.ShopOrder{OrderId: orderId}
	err := userOrder.Load()
	if err != nil {
		orderController.RetCode = -1
		orderController.Msg = err.Error()
		ctx.JSON(200, orderController.Result)
		return
	}
	err = userOrder.Finish()
	if err != nil {
		orderController.RetCode = -1
		orderController.Msg = err.Error()
		ctx.JSON(200, orderController.Result)
		return
	}
	orderController.Data = gin.H{
		"orderId": userOrder.OrderId,
	}
	ctx.JSON(200, orderController.Result)

}
